package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

var x int64
var wg sync.WaitGroup

// 原子类 int32 int64
// loadT
// StoreT
// AddT(ptr *T, addNum T)
// CompareAndSwapT(*ptr, oldVal, newVal)

func add() {
	defer wg.Done()
	// x++ // concurrent write int
	atomic.AddInt64(&x, 1)
}

func main() {
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go add()
	}
	wg.Wait()

	fmt.Println(atomic.LoadInt64(&x))

	fmt.Println(atomic.CompareAndSwapInt64(&x, 100, 200))
	fmt.Println(atomic.LoadInt64(&x))
}
